from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QListView
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QCheckBox
from PyQt5.QtWidgets import QGroupBox
from PyQt5.QtWidgets import QAbstractItemView
from PyQt5.QtWidgets import QWidget


class RemoteHook(QWidget):
    def __init__(self, hook_manager, log_out):
        super(QWidget, self).__init__()
        self.log_out = log_out
        self.hook_manager = hook_manager
        self.method_list = list()
        self.method_mapping = dict()
        self.is_pause = True

        lb_class = QLabel('请输入类名:')
        self.lt_class = QLineEdit()

        self.cb_batch = QCheckBox('批量挂钩')
        self.cb_batch.stateChanged.connect(self.batch_click)
        self.cb_overload = QCheckBox('重载函数挂钩')
        self.cb_overload.stateChanged.connect(self.overload_click)

        self.btn_method = QPushButton('获取类方法')
        self.btn_method.clicked.connect(self.btn_method_click)

        self.btn_hook = QPushButton('方法挂钩')
        self.btn_hook.clicked.connect(self.btn_hook_click)

        h_box = QHBoxLayout()
        h_box.addWidget(lb_class)
        h_box.addWidget(self.lt_class)
        h_box.addWidget(self.cb_batch)
        h_box.addWidget(self.cb_overload)
        h_box.addWidget(self.btn_method)
        h_box.addWidget(self.btn_hook)

        self.list_view = QListView()
        self.list_view.setEditTriggers(QAbstractItemView.NoEditTriggers)

        temp_box = QHBoxLayout()
        temp_box.addWidget(self.list_view)

        temp_group_box = QGroupBox('函数列表')
        temp_group_box.setLayout(temp_box)

        h_list_box = QHBoxLayout()
        h_list_box.addWidget(temp_group_box)

        self.text_view = QTextEdit()
        self.text_view.setFontFamily('Consolas')
        self.text_view.setFontPointSize(12)
        temp_box = QHBoxLayout()
        temp_box.addWidget(self.text_view)

        temp_group_box = QGroupBox('输出结果')
        temp_group_box.setLayout(temp_box)

        h_text_box = QHBoxLayout()
        h_text_box.addWidget(temp_group_box)

        v_box = QVBoxLayout()
        v_box.addLayout(h_box)
        v_box.addLayout(h_list_box)
        v_box.addLayout(h_text_box)
        self.setLayout(v_box)

    def batch_click(self):
        if self.cb_batch.isChecked():
            self.list_view.setSelectionMode(QAbstractItemView.MultiSelection)
            self.cb_overload.setEnabled(False)
        else:
            self.list_view.setSelectionMode(QAbstractItemView.SingleSelection)
            self.cb_overload.setEnabled(True)

    def overload_click(self):
        if self.cb_overload.isChecked():
            self.cb_batch.setEnabled(False)
        else:
            self.cb_batch.setEnabled(True)

    def update_list_view(self, data):
        pass

    def update_text_view(self, data):
        pass

    def insert_class_info(self, data):
        self.log_out.insert(data)

    def btn_method_click(self):
        pass

    def btn_hook_click(self):
        pass

    def rpc_get_class_methods(self, class_name):
        pass

    def rpc_get_hook_methods(self, methods):
        pass

    def get_overload_methods(self, text):
        pass

    def on_message(self, message, data):
        pass

    @staticmethod
    def analysis_method_name(text):
        text = text.split('(')
        text = text[0]
        start = text.rfind('.')
        return text[start + 1:]
